 package com.ysd.impl;

import java.util.ArrayList;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ysd.dao.PermissionRepository;
import com.ysd.service.PermissionService;

@Service
public class PermissionServiceImpl implements PermissionService {

	@Autowired
	private PermissionRepository pr;




	@Override
	public Object getAllPermissions(Object id) {
		// TODO Auto-generated method stub
		List<Object> perids=pr.getPerIdsByRoleId(id);
		List<Object> titles=pr.getTitles();
		List<Map<String, Object>> m=new ArrayList<Map<String,Object>>();
		
		String ti=null;
		
		for (int i = 0; i < titles.size(); i++) {
			ti = (String) titles.get(i);
			List<String> childtitle=pr.getChildByTitle(ti);
			System.out.println(childtitle.size());
			Object id2 =null;
			List<Map<String, Object>> childrens=new ArrayList<Map<String,Object>>();
			for (int j = 0; j < childtitle.size(); j++) {
				Map<String, Object> map4 = new HashMap<String, Object>();
				id2= pr.getChildIdByChildTitle(childtitle.get(j),ti).get(0);
				if (perids.contains(id2)) {
					map4.put("checked", true);
				}
				map4.put("title", childtitle.get(j));
				map4.put("id", id2);
				childrens.add(map4);
			}
			System.out.println(childrens);
			Map<String, Object> map3 = new HashMap<String, Object>();
			List<Object> ids=pr.getIdsByTitle(titles.get(i));
			map3.put("title", ti);
			map3.put("id", ids);
			map3.put("children", childrens);
			m.add(map3);
		}
		return m;
	}




	@Override
	public List<Object> findPreByR_P(Object rid, String pids) {
		// TODO Auto-generated method stub
		return pr.findPreByR_P(rid,pids);
	}




	@Override
	public Integer addR_P(Object rid, String a) {
		// TODO Auto-generated method stub
		return pr.addR_P(rid, a);
	}
	/**
	 2 * 去除首尾指定字符
	 3 * @param str 字符串
	 4 * @param element 指定字符
	 5 * @return
	 6 */
	 public String trimFirstAndLastChar(String str, String element){
	      boolean beginIndexFlag = true;
	      boolean endIndexFlag = true;
	     do{
	         int beginIndex = str.indexOf(element) == 0 ? 1 : 0;
	         int endIndex = str.lastIndexOf(element) + 1 == str.length() ? str.lastIndexOf(element) : str.length();
	         str = str.substring(beginIndex, endIndex);
	         beginIndexFlag = (str.indexOf(element) == 0);
	         endIndexFlag = (str.lastIndexOf(element) + 1 == str.length());
	     } while (beginIndexFlag || endIndexFlag);
	     return str;
	 }




	@Override
	public Integer delR_P(Object rid, String a) {
		// TODO Auto-generated method stub
		return pr.delR_P(rid,a);
	}




	@Override
	public List<Object> findPresByRid(Object rid) {
		// TODO Auto-generated method stub
		return pr.getPerIdsByRoleId(rid);
	}
	
}